package com.piik.mall.product.service.impl;

import com.piik.mall.product.entity.AttrEntity;
import com.piik.mall.product.service.AttrService;
import com.piik.mall.product.vo.AttrGroupWithAttsVO;
import com.piik.mall.product.vo.SpuItemGroupAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piik.common.utils.PageUtils;
import com.piik.common.utils.Query;

import com.piik.mall.product.dao.AttrGroupDao;
import com.piik.mall.product.entity.AttrGroupEntity;
import com.piik.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        //拼接查询条件
        if(!StringUtils.isEmpty(key)){
            wrapper.and(obj->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        //不根据catelogId来查询
        if(catelogId==0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }

        //根据类别编号来查询属性信息
        wrapper.eq("catelog_id",catelogId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 1. 根据分类编码查询属性组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttsVO> list = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttsVO attrGroupWithAttsVO = new AttrGroupWithAttsVO();
            BeanUtils.copyProperties(item, attrGroupWithAttsVO);
            // 2. 根据属性组找到所有属性信息
            List<AttrEntity> attrEntities = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupWithAttsVO.setAttrs(attrEntities);
            return attrGroupWithAttsVO;
        }).collect(Collectors.toList());
       return list;
    }
    /**
     * 跟进SpuId和CatalogId查询出对应的 属性组及其属性信息
     * @param spuId
     * @param catalogId
     * @return
     */
    @Override
    public List<SpuItemGroupAttrVo> getAttrgroupWithSpuId(Long spuId, Long catalogId) {
        List<SpuItemGroupAttrVo> groupAttrVo = attrGroupDao.getAttrgroupWithSpuId(spuId,catalogId);
        return groupAttrVo;

    }

}